package office.word.openoffice;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import org.jodconverter.core.office.OfficeUtils;
import org.jodconverter.local.JodConverter;
import org.jodconverter.local.office.LocalOfficeManager;

import java.io.*;

/**
 * @author zhanglu
 * @project JavaBase
 * @description
 * @date 2023/6/5 11:51
 */
public class TestController {

    /**
     * 默认转换后文件后缀
     */
    private static final String DEFAULT_SUFFIX = "pdf";
    /**
     * openoffice_port
     */
    private static final Integer OPENOFFICE_PORT = 8100;

    public static void main(String[] args) throws Exception {
        //docx 转 html
        String source = "/Users/zhanglu/Desktop/tmp/docx4j/word_template2.docx";
        String target = null;
        //doc 转 html
//        source = "/Users/zhanglu/Desktop/111.doc";
//        target = "/Users/zhanglu/Desktop/111.html";
//        //html 转 doc
//        source = target;
//        target = "/Users/zhanglu/Desktop/tmp/jod/e基建2.0电子签章使用组件分析0529-02.doc";
//        //docx 转 pdfFilePreview
////        source = "/Users/zhanglu/Desktop/tmp/jod/e基建2.0电子签章使用组件分析0529.docx";
////        target = "/Users/zhanglu/Desktop/tmp/jod/e基建2.0电子签章使用组件分析0529-03.pdf";

//        String random = DateUtil.format(DateUtil.date(), "yyyy-MM-ddHHmmss") + "_" + RandomUtil.randomNumbers(2);
//        String tmpDir = "/Users/zhanglu/Desktop/tmp/" + random;
//        FileUtil.mkdir(tmpDir);
//        target = tmpDir + File.separator + RandomUtil.randomNumbers(4) + ".html";
//        fileConvert(source, target);
//        String html = IoUtil.read(new FileInputStream(target), CharsetUtil.UTF_8);
//        html = htmlImageConvert(html, tmpDir, random);
//        String newHtml = tmpDir + File.separator + RandomUtil.randomNumbers(4) + ".html";
//        FileUtil.writeUtf8String(html, new File(newHtml));
//        //doc 转 html
//        source = newHtml;
        target = "/Users/zhanglu/Desktop/tmp/docx4j/word_template2.pdf";
        fileConvert(source, target);

//        //实例化Document类的对象
//        Document  doc = new Document();
//        //加载Word文档
//        doc.loadFromFile("/Users/zhanglu/Desktop/tmp/spire/e基建2.0电子签章使用组件分析0529.docx");
//        //保存为HTML格式
//        doc.saveToFile("/Users/zhanglu/Desktop/tmp/spire/1.html",FileFormat.Html);
//        doc.dispose();

//        //spire实现，不好
//        //新建Document对象
//        Document document = new Document();
//        //添加section
//        Section sec = document.addSection();
//        //添加段落并写入HTML文本
//        sec.addParagraph().appendHTML(FileUtil.readUtf8String("/Users/zhanglu/Desktop/tmp/spire/1.html"));
//        document.saveToFile("/Users/zhanglu/Desktop/tmp/spire/2.docx", FileFormat.Docx);

//        Document document = new Document("/Users/zhanglu/Desktop/tmp/aspose/source.docx");
//        document.save("/Users/zhanglu/Desktop/tmp/aspose/1.html");

    }

    public static void fileConvert(String source, String target){
        LocalOfficeManager officeManager = null;
        try {
            officeManager = LocalOfficeManager.install();
            officeManager.start();
            JodConverter.convert(new File(source)).to(new File(target)).execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            OfficeUtils.stopQuietly(officeManager);
        }
    }

    public static void fileConvert(InputStream source, String target){
        LocalOfficeManager officeManager = null;
        try {
            officeManager = LocalOfficeManager.install();
            officeManager.start();
            JodConverter.convert(source).to(new File(target)).execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            OfficeUtils.stopQuietly(officeManager);
        }
    }

    public static OutputStream fileConvert(InputStream source, OutputStream target){
        LocalOfficeManager officeManager = null;
        try {
            officeManager = LocalOfficeManager.install();
            officeManager.start();
            JodConverter.convert(source).to(target);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            OfficeUtils.stopQuietly(officeManager);
        }
        return target;
    }

    public static String htmlImageConvert(String html, String tmpDir, String random){
        for (File loopFile : FileUtil.loopFiles(tmpDir, o -> FileUtil.extName(o.getName()).equalsIgnoreCase("png")
                || FileUtil.extName(o.getName()).equalsIgnoreCase("jpg")
                || FileUtil.extName(o.getName()).equalsIgnoreCase("jpeg"))) {
            String name = loopFile.getName();
//            html = StrUtil.replace(html, name, "http://ticdfr.natappfree.cc/" + random + "/" + name);
            html = StrUtil.replace(html, name, "data:image/jpeg;base64," + Base64.encode(loopFile));
        }
        return html;
    }

}
